package com.gaobug.platform.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaobug.base.context.JwtContext;
import com.gaobug.base.exception.BusinessException;
import com.gaobug.platform.dao.CommentInfoMapper;
import com.gaobug.platform.domain.CommentInfo;
import com.gaobug.platform.dto.CommentDTO;
import com.gaobug.platform.dto.VoteDTO;
import com.gaobug.platform.service.CommentInfoService;
import com.gaobug.platform.vo.CommentVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author huang
 * @description 针对表【t_comment_info(评论表)】的数据库操作Service实现
 * @createDate 2022-12-14 16:38:53
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {

    @Resource
    private CommentInfoMapper commentMapper;

    @Override
    public void commentBlog(CommentDTO commentDTO) {
        CommentInfo commentOfSave = new CommentInfo();
        // 设置顶级评论标识
        if (0 != commentDTO.getLinkId()) {
            CommentInfo commentInfo = commentMapper.selectById(commentDTO.getLinkId());
            if (commentInfo == null) {
                throw new BusinessException("该评论已删除，无法继续评论");
            }
            commentOfSave.setTopId(commentInfo.getTopId());
        }
        commentOfSave.setUserId(JwtContext.getUserId());
        commentOfSave.setLinkId(commentDTO.getLinkId());
        commentOfSave.setArticleId(commentDTO.getArticleId());
        commentOfSave.setContext(commentDTO.getContext());
        commentOfSave.setCreateTime(new Date());
        commentOfSave.setDelFlag(false);
        commentMapper.insert(commentOfSave);
    }

    @Override
    public IPage<CommentVO> commentPage(String articleId,Page<CommentInfo> page) {
        return commentMapper.selectCommentPage(page,articleId);
    }

    @Override
    public IPage<CommentVO> commentExpandPage(String commentId, Page<CommentInfo> page) {
        return commentMapper.selectCommentExpandPage(page, commentId);
    }

    @Override
    public void commentDel(Long id) {
        CommentInfo commentOfDel = new CommentInfo();
        commentOfDel.setId(id);
        commentOfDel.setDelFlag(true);
        commentMapper.updateById(commentOfDel);
    }

    @Override
    public void commentLike(VoteDTO voteDTO) {

    }
}




